var app = getApp();
Page({
    data: {
        apis: app.data.ipconfig,
        loading: '',
        selthis:'redLine',
        isSelect:0,
        isShow:false,//是否显示弹出层
        isUp:"up",//按照价格排序，down安装降序
        currentShaiXuan:[],
        
    },
    onLoad: function () {
        var that = this;
       
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    pageheight:(res.windowHeight)*2-80,
                    pagescorll:(res.windowHeight)*2-180,
                });
            }
        })
     
        wx.request({
          url: that.data.apis + '/wxxiaochengxu/homebannerApi/goodstype',
          data: {appid: app.data.appid},
          header: {
            'content-type': 'application/json'
          },
          success:function (res){
            console.log(res);

            var list = new Array();
            list.push.apply(list,res.data.res.goodslist);
            console.log(list)
            list.push.apply(list,res.data.res.goodslist);
            list.push.apply(list,res.data.res.goodslist);
            list.push.apply(list,res.data.res.goodslist);
            list.push.apply(list, res.data.res.goodslist);
            list.push.apply(list, res.data.res.goodslist);
            list.push.apply(list,res.data.res.goodslist);
            list.push.apply(list,res.data.res.goodslist);
            list.push.apply(list, res.data.res.goodslist);
            console.log(list)

            
            that.setData({
              typeList:res.data.res,
              goodsList:  res.data.res.goodslist
            })

            var typeList = res.data.res.goodstypeList;
            // 取第一个的listid
            if (typeList.length>0){
              that.setData({
                isSelect: typeList[0].goodTypesId
                //让的一个直接是选中的状态
              });
            }
          },
          fail: function () {
            console.log("接口出现异常");
          }
        }),
          wx.request({
            url: that.data.apis + '/wxxiaochengxu/homebannerApi/specQuery',
            data: {
              appid: app.data.appid,
              openid: wx.getStorageSync('openid'),
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res.data.res.speclist);
              that.setData({
                speclist: res.data.res.speclist,
                loading: 'hidden'
              })
            },
            fail: function () {
              console.log("接口出现异常");
            }
          })
    },
    //点击左侧分类按钮
    clickOther:function(e){
      var typeId = e.currentTarget.dataset.typeid;
      var that = this;
      console.log(e);
      that.setData({
        isSelect: typeId
      });
      console.log(that.data);
      //发请求
      wx.request({
        url: that.data.apis + '/wxxiaochengxu/homebannerApi/getGoodsByTypeId',
        data: { appid: app.data.appid, goodTypesId: typeId},
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res);
          that.setData({
            goodsList: res.data.res.goodsList
          })

        },
        fail: function () {
          console.log("接口出现异常");
        }
      })

    },
    clickXiaoLiang:function(){
      //发请求只按销量
      var that = this;
      console.log(that.data.isSelect);
      wx.request({
        url: that.data.apis + '/wxxiaochengxu/homebannerApi/getGoods',
        data: { appid: app.data.appid, goodTypesId: that.data.isSelect, flag:1 },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res);
          that.setData({
            goodsList: res.data.res
          })

        },
        fail: function () {
          console.log("接口出现异常");
        }
      })

    },
    //按照价格排序
    clickPrice:function(e){
      var that = this;
      //获取当前的状态
      if (e.currentTarget.dataset.type == 'up'){
        that.data.isUp = 'down';
      }else{
        that.data.isUp = 'up';
      }
      console.log(that.data.isUp);
      wx.request({
        url: that.data.apis + '/wxxiaochengxu/homebannerApi/getGoods',
        data: { appid: app.data.appid, goodTypesId: that.data.isSelect, flag: that.data.isUp },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res);
          that.setData({
            goodsList: res.data.res,
            isUp: that.data.isUp
          })

        },
        fail: function () {
          console.log("接口出现异常");
        }
      })
    },

    //筛选弹出层
    screentab: function () {
      var that = this;
      //先获取对应的筛选条件
      wx.request({
        url: that.data.apis + '/wxxiaochengxu/homebannerApi/getSubTypes',
        data: { appid: app.data.appid, goodTypesId: that.data.isSelect},
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res);
          that.setData({
            shaiXuanList: res.data.res
          })

        },
        fail: function () {
          console.log("接口出现异常");
        }
      })


      that.setData({
        isShow: (!that.data.isShow)
      })
    },
    //选择筛选的类型
    selText:function(e){
      console.log(e);
      var that = this;
      var index = e.currentTarget.dataset.index;
      //遍历当前的规格里面有没有该id有的话 就
      var newList = that.data.shaiXuanList
      console.log(newList);
      for (var i = 0; i < newList.length;i++){
        var obj = newList[i];
        if (obj.goodTypesId == index){
          console.log("相等");

            if (obj.isSelect == false || obj.isSelect == null) {
              console.log("当前是非选中状态");
              newList[i].isSelect = true;
            } else {
              console.log("当前是选中状态");
              newList[i].isSelect = false;
            }
            
        }
      }
      console.log(newList);
      that.setData({
        shaiXuanList: newList
      })
    },
    //点击完成按钮
    clickFinish:function(){
      var that = this;
      //发送请求
      //先获取拼接type
      var typeList = [];
      for (var i = 0; i < that.data.shaiXuanList.length; i++) {
        //
        var obj = that.data.shaiXuanList[i];
        if(obj.isSelect == true){
          typeList.push(obj.goodTypesId);
        }
      }
      wx.request({
        url: that.data.apis + '/wxxiaochengxu/homebannerApi/getGoods',
        data: { appid: app.data.appid, goodTypesId: that.data.isSelect, flag: 2, types: typeList},
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res);
          that.setData({
            goodsList: res.data.res,
            isShow:false,
            shaiXuanList: []
          })

        },
        fail: function () {
          console.log("接口出现异常");
        }
      })

    },

    reload:function(){
      var that = this;
      var newList = that.data.shaiXuanList
      for (var i = 0; i < newList.length; i++) {
        newList[i].isSelect = false;
      }
      that.setData({
        shaiXuanList: newList
      })
    },

    clickGoods:function(e){
      //跳转到商品详情页去
      var shopid = e.currentTarget.dataset.shopid;
      wx.navigateTo({
        url: '../../pages/shopDetails/index?shopid=' + shopid
      })

    },
    closeView:function(){
      var that = this;
      that.setData({
        isShow: false,
        shaiXuanList: []
      })
    },

    nocloseView: function () {
      event.stopPropagation();
    },




    onPullDownRefresh: function() {
        console.log('刷新');
        var that=this;
        setTimeout(function(){
            wx.stopPullDownRefresh();
            that.onLoad();
        },2000)
    }
})
;

